package argtrust.certprop;

import argtrust.measure.IMeasureOperators;
import argtrust.measure.Measure;

/**
 * 
 * @author Chung-Wei Hang
 *
 */
public class CertPropMeasureOperators extends IMeasureOperators {

    private double getLevel(Measure measure) //            throws ArgTrustException
    {
        if (!(measure instanceof CertPropMeasure)) {
            //TODO: throw an exception.
            System.err.println("CertPropMeasureComparator: Not a CertProp-measurement");
            if (measure != null) {
                System.err.println("measure type:" + measure.getClass().getName());
            }
            try {
                throw new argtrust.basics.ArgTrustException("CertPropMeasureOperators: Not a CertProp-measurement");
            } catch (argtrust.basics.ArgTrustException e) {
                e.printStackTrace();
            }
            System.exit(-1);
            return -1;
        }
        return ((CertPropMeasure) measure).b();
    }

    public CertPropMeasure update(CertPropMeasure t1, CertPropMeasure t2) {
        double p2 = t2.p();
        double p1 = t1.p();
        double c1 = t1.c();
        double a = Math.pow(p2, t2.r()) * Math.pow((1 - p2), t2.s());
        double b = Math.pow(p1, t2.r()) * Math.pow((1 - p1), t2.s());
        double q = b / a;

        return new CertPropMeasure(c1 * q, c1 * (1 - q));
    }

    public CertPropMeasure concatenate(CertPropMeasure t1, CertPropMeasure t2) {
        double b1 = t1.b();
        return new CertPropMeasure(b1 * t2.r(), b1 * t2.s());
    }

    public CertPropMeasure aggregate(CertPropMeasure t1, CertPropMeasure t2) {
        return new CertPropMeasure(t1.r() + t2.r(), t1.s() + t2.s());
    }

//    @Override
//    public boolean eq(Measure lhs, Measure rhs) {
//        return lhs.equals(rhs);
//    }

    @Override
    public boolean eq(Measure lhs, Measure rhs) {
        return (getLevel(lhs) == getLevel(rhs));
    }
    @Override
    public boolean lt(Measure lhs, Measure rhs) {
        return (getLevel(lhs) < getLevel(rhs));
    }
//    @Override
//    public boolean lt(Measure lhs, Measure rhs) {
//        CertPropMeasure l = (CertPropMeasure) lhs;
//        CertPropMeasure r = (CertPropMeasure) lhs;
//        return (l.b() < r.b())
//                || ((l.b() == r.b())
//                && ((l.d() > r.d())
//                || (l.d() == r.d()
//                && l.u() < r.u())));
//    }
}
